package com.gaswell.utils;

import com.alibaba.excel.annotation.ExcelProperty;
import com.gaswell.entity.Qba01;
import com.gaswell.entity.Qba01M;
import com.gaswell.pojo.*;
import com.gaswell.vo.Qfc01Vo;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName ObjectUtils
 * @Author xiaow
 * @DATE 2023/2/21 15:36
 **/

public class ObjectUtils {
    //    是否正在写入
    public static Object instance = new Object();

    public static Qba01M qba01ToQba01M(Qba01 qba01) {

        Qba01M qba01_m = new Qba01M().setA2jh(qba01.getA2jh())
                .setBtyl(qba01.getBtyl())
                .setBz(qba01.getBz())
                .setCqdb(qba01.getCqdb())
                .setCqwd(qba01.getCqwd())
                .setCqyc(qba01.getCqyc())
                .setCqyl(qba01.getCqyl())
                .setCsdm(qba01.getCsdm())
                .setCsdm1(qba01.getCsdm1())
                .setDm(qba01.getDm())
                .setDzd(qba01.getDzd())
                .setGjdm(qba01.getGjdm())
                .setGjty(qba01.getGjty())
                .setGjyy(qba01.getGjyy())
                .setJgty(qba01.getJgty())
                .setJh(qba01.getJh())
                .setJkwd(qba01.getJkwd())
                .setJtyl(qba01.getJtyl())
                .setJyty(qba01.getJyty())
                .setPjty(qba01.getPjty())
                .setPjyy(qba01.getPjyy())
                .setQcdja2(qba01.getQcdja2())
                .setQcdk(qba01.getQcdk())
                .setQcdka2(qba01.getQcdka2())
                .setQm(qba01.getQm())
                .setQzzj(qba01.getQzzj())
                .setRcol(qba01.getRcol())
                .setRcql(qba01.getRcql())
                .setRcsl(qba01.getRcsl())
                .setRq(qba01.getRq())
                .setRzc(qba01.getRzc())
                .setScsj(qba01.getScsj())
                .setScsjj(qba01.getScsjj())
                .setWsyl(qba01.getWsyl())
                .setZdty(qba01.getZdty())
                .setZdyy(qba01.getZdyy())
                .setZgty(qba01.getZgty())
                .setZgyy(qba01.getZgyy())
                .setZm(qba01.getZm())
                .setZyq(qba01.getZyq())
                .setZyql(qba01.getZyql());
        return qba01_m;
    }


//    public static Qba01 qba01ToQba012(Qba01_bak qba01) {
//
//        Qba01 qba01_m = new Qba01().setA2jh(qba01.getA2jh())
//                .setBtyl(qba01.getBtyl())
//                .setBz(qba01.getBz())
//                .setCqdb(qba01.getCqdb())
//                .setCqwd(qba01.getCqwd())
//                .setCqyc(qba01.getCqyc())
//                .setCqyl(qba01.getCqyl())
//                .setCsdm(qba01.getCsdm())
//                .setCsdm1(qba01.getCsdm1())
//                .setDm(qba01.getDm())
//                .setDzd(qba01.getDzd())
//                .setGjdm(qba01.getGjdm())
//                .setGjty(qba01.getGjty())
//                .setGjyy(qba01.getGjyy())
//                .setJgty(qba01.getJgty())
//                .setJh(qba01.getJh())
//                .setJkwd(qba01.getJkwd())
//                .setJtyl(qba01.getJtyl())
//                .setJyty(qba01.getJyty())
//                .setPjty(qba01.getPjty())
//                .setPjyy(qba01.getPjyy())
//                .setQcdja2(qba01.getQcdja2())
//                .setQcdk(qba01.getQcdk())
//                .setQcdka2(qba01.getQcdka2())
//                .setQm(qba01.getQm())
//                .setQzzj(qba01.getQzzj())
//                .setRcol(qba01.getRcol())
//                .setRcql(qba01.getRcql())
//                .setRcsl(qba01.getRcsl())
//                .setRq(ObjectUtils.dateFormat2Date(qba01.getRq()))
//                .setRzc(qba01.getRzc())
//                .setScsj(qba01.getScsj())
//                .setScsjj(qba01.getScsjj())
//                .setWsyl(qba01.getWsyl())
//                .setZdty(qba01.getZdty())
//                .setZdyy(qba01.getZdyy())
//                .setZgty(qba01.getZgty())
//                .setZgyy(qba01.getZgyy())
//                .setZm(qba01.getZm())
//                .setZyq(qba01.getZyq())
//                .setZyql(qba01.getZyql());
//        return qba01_m;
//    }

    public static ALLQj QjToAllQj(ALLQj allQj, Qj qj) {
        allQj.setTxbh(qj.getTxbh())
                .setJwd(qj.getJwd())
                .setJkbz(qj.getJkbz())
                .setJxj(qj.getJxj())
                .setSfszhjs(qj.getSfszhjs())
                .setLxrxm(qj.getLxrxm())
                .setLxrdh(qj.getLxrdh());
        return allQj;
    }

    public static String dateFormat(String date) {
        String s = date.replaceAll("/", "-");
        String[] split = s.split("-");
        StringBuilder stringBuilder = new StringBuilder();
        for (String s1 : split) {
            if (Integer.parseInt(s1) < 10 && s1.length() < 2)
                s1 = "0" + s1;
            stringBuilder.append(s1 + "-");

        }
        stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
        return stringBuilder.toString();
    }


    public static Date dateFormat2Date(String date) {
        String s = date.replaceAll("/", "-");
        String[] split = s.split("-");
        StringBuilder stringBuilder = new StringBuilder();
        for (String s1 : split) {
            if (Integer.parseInt(s1) < 10 && s1.length() < 2)
                s1 = "0" + s1;
            stringBuilder.append(s1 + "-");

        }
        stringBuilder.delete(stringBuilder.length() - 1, stringBuilder.length());
        String s1 = stringBuilder.toString();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        java.util.Date parse = null;
        try {
            parse = simpleDateFormat.parse(s1);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return new Date(parse.getTime());
    }


    public static <E> E getListOne(List<E> list) {
        if (list != null && list.size() > 0)
            return list.get(0);
        return null;
    }

    private static String wordToUpperCase(String str) {
        // 使用空格将句子切分成单词
        String[] strArr = str.split(" ");
        StringBuffer strBuf = new StringBuffer();
        // 遍历取出每个单词，将首字母转换为大写
        for (String string : strArr) {
            String upperStr = string.substring(0, 1).toUpperCase() + string.substring(1);
            strBuf.append(upperStr);
            strBuf.append(" ");
        }
        // 将转换后的字符串数组转换为字符串
        String retStr = new String(strBuf);
        return retStr;
    }


    public static <E> E setNull(E o) {
        Class<?> aClass = o.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Class<?> type = field.getType();
            String name = field.getName();
            try {
                if (type.toString().equals("class java.lang.String")) {
                    String o1 = (String) field.get(o);
                    if (o1 != null && o1.equals("")) {
                        field.set(o, null);
                    }
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return o;
    }

    public static <E> List<String> getNames(E o) {
        LinkedList<String> strings = new LinkedList<>();
        Class<?> aClass = o.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Class<?> type = field.getType();
            String name = field.getName();
            try {
                if (type.toString().equals("class java.lang.String")) {
                    String o1 = (String) field.get(o);
                    if (o1 != null && !o1.equals("")) {
                        strings.add(name);
                    }
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return strings;
    }


    public static <E> Map<String, Map<String, String>> getAnonations(E o) {
//       属性名   注解值   内容
        Map<String, Map<String, String>> strings = new HashMap<>();
        Class<?> aClass = o.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Class<?> type = field.getType();
            String name = field.getName();
            try {
                if (type.toString().equals("class java.lang.String")) {
                    String o1 = (String) field.get(o);
                    if (o1 != null && !o1.equals("")) {
                        String[] value = field.getAnnotation(ExcelProperty.class).value();
                        if (value != null && value.length > 0) {
                            HashMap<String, String> stringStringHashMap = new HashMap<>();
                            stringStringHashMap.put(value[0], o1);
                            strings.put(name, stringStringHashMap);
                        }

                    }
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return strings;
    }

    public static <E> List<String> getValues(E o) {
        LinkedList<String> strings = new LinkedList<>();
        Class<?> aClass = o.getClass();
        Field[] fields = aClass.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            Class<?> type = field.getType();
            String name = field.getName();
            try {
                if (type.toString().equals("class java.lang.String")) {
                    String o1 = (String) field.get(o);
                    if (o1 != null && !o1.equals("")) {
                        strings.add(o1);
                    }
                }

            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        return strings;
    }


    public static Qfc01Vo getQfc01(List<Qba01> list) {
        LinkedList<String> jhs2 = new LinkedList<>();
        String start = null;
        String end = null;
        HashSet<String> jhs = new HashSet<String>();
        for (Qba01 qba01M : list) {
            if (start == null || DateUtils.sqldateToString(qba01M.getRq()).compareTo(start) < 0) {
                start = DateUtils.sqldateToString(qba01M.getRq());
            }
            if (end == null || DateUtils.sqldateToString(qba01M.getRq()).compareTo(end) > 0) {
                end = DateUtils.sqldateToString(qba01M.getRq());
            }
            jhs.add(qba01M.getJh());
        }
        jhs.forEach(jh -> {
            jhs2.add(jh);
        });
        return new Qfc01Vo()
                .setJhs(jhs2)
                .setEnd(end)
                .setStart(start);
    }


    public static double bigDecimal2double(BigDecimal data) {
        return data == null ? 0 : data.doubleValue();
    }

    public static String getBinaryCommand(List<ModbusLog> list) {
        return null;
    }

    /**
     * 拿到Rtudata list中的所有井号
     *
     * @param list
     * @return
     */
    public static List<String> getRtuDataJhs(List<RtuData> list) {
        LinkedList<String> strings = new LinkedList<>();
        for (RtuData rtuData : list) {
            strings.add(rtuData.getJh());
        }
        return strings;
    }


    /**
     * byte[]转int
     *
     * @param bytes
     * @return
     */
    public static int byteToInt(byte bytes) {
        int shift = (4 - 1) * 8;
        return (bytes & 0x000000FF) << shift;
    }

    public static void main(String[] args) {
    }
}
